package cn.tedu.javagame.Plane;

import javax.swing.*;
import java.awt.*;

public class Bullet {
    int x,y;
    static Image bulletImg;
    static {
        bulletImg = new ImageIcon("plane/bullets.png").getImage();
    }
    public Bullet(int x,int y){
        this.x=x;
        this.y=y;

    }
    public void move(){
        this.y-=5;
    }

    public boolean hitEnemy(Enemy enemy){
        // 获取英雄和敌人的边界
        int bulletLeft = this.x;
        int bulletRight = this.x + this.bulletImg.getWidth(null);
        int bulletTop = this.y;
        int bulletBottom = this.y + this.bulletImg.getHeight(null);

        int enemyLeft = enemy.x;
        int enemyRight = enemy.x + enemy.img.getWidth(null);
        int enemyTop = enemy.y;
        int enemyBottom = enemy.y + enemy.img.getHeight(null);

        // 检测两个矩形是否重叠
        boolean isOverlap = bulletLeft < enemyRight &&
                bulletRight > enemyLeft &&
                bulletTop < enemyBottom &&
                bulletBottom > enemyTop;
        if (isOverlap) {
            System.out.println("击落敌机");

            return true;
        }

        return false;
    }
}